$(function () {
    // 默认加载部门树
    vm.getDept();
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "name" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    vm.q.deptId = treeObj.deptId;
    vm.search();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            month: '', // 统计月份
            deptId: '01' // 设备组织CODE
        },
        option: {
            tooltip: {
                trigger: 'axis'
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: {show: true, type: ['bar']},
                    dataView: {show: true, readOnly: false},
                    saveAsImage: {show: true}
                },
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function (params) {
                    var tar = params[1];
                    return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
                }
            },
            legend: {
                data: ['故障处理时间', '故障时间', '运行时间']
            },
            xAxis: [
                {
                    type: 'category',
                    data: []
                }
            ],
            yAxis: [
                {type: 'value'},
                {type: 'value'}
            ],
            series: [
                {
                    name: '故障处理时间',
                    type: 'bar',
                    data: []
                },
                {
                    name: '故障时间',
                    type: 'bar',
                    data: []
                },
                {
                    name: '运行时间',
                    type: 'line',
                    yAxisIndex: 1,
                    data: []
                }
            ]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node) {
                        ztree.selectNode(node);
                        ztree.expandNode(node);
                    }else{
                        ztree.expandAll(true);
                    }
                }
            });
        },
        search: function () {
            var param = {};
            if (this.q.month) {
                param.month = this.convertGMT2Month(this.q.month);
            }
            if (this.q.deptId) {
                param.deptId = this.q.deptId;
            }
            myChart.showLoading();
            // 加载设备状态概览
            Ajax.request({
                url: '../device/stat/faultDurationAnalyse',
                type: 'POST',
                params: JSON.stringify(param),
                contentType: 'application/json',
                successCallback: function (res) {
                    myChart.hideLoading();
                    // 故障时长统计
                    var faultDurationStat = res.result;
                    var option = {
                        xAxis: {
                            data: faultDurationStat.xaxisData
                        },
                        series: [
                            {data: faultDurationStat.barSeriesData[0].data},
                            {data: faultDurationStat.barSeriesData[1].data},
                            {data: faultDurationStat.barSeriesData[2].data}
                        ]
                    };
                    myChart.setOption(option);
                }
            });
        },
        reset: function () {
            this.q = {
                month: this.getCurrentMonth(), // 统计月份
                deptId: '01' // 设备组织CODE
            };
            this.search();
        },
        convertGMT2Month: function (time) {
            var date = new Date(time)
            var month = (date.getMonth() + 1);
            return date.getFullYear() + '-' + (month < 10 ? '0' : '') + month;
        },
        getCurrentMonth: function () {
            var date = new Date()
            var month = (date.getMonth() + 1);
            return date.getFullYear() + '-' + (month < 10 ? '0' : '') + month;
        }
    },
    mounted: function () {
        // 初始化处理时长分析报表
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option, true);
        // 初始化加载数据
        this.search();
    },
    created: function () {
        this.q.month = this.getCurrentMonth();
    }
});
